﻿#region Using directives
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProcolDemo.InterestReference;
using System.Collections.Generic;


#endregion

namespace ProcolDemo {
    public partial class ManageInterests : Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Common.IsUserLoggedIn(Session))
                Response.Redirect("Default.aspx");

            Common.FillI18NStrings(this, Session);

            int languageId = Common.GetUserLanguage(Common.GetUserId(Session));

            int uidSession = Common.GetUserId(Session);
            int uidRequest = Common.GetUserId(Request);

            InterestService interestService = new InterestService();

            /* DISPLAY CATEGORIES AND INTERESTS */
            CategoryBean[] categoriesList = interestService.GetCategoriesList(languageId);
            CheckBoxList chbxList = new CheckBoxList();
            for(int i=0; i<categoriesList.Length; i++){
                Label lbl1 = new Label();
                lbl1.Text = "<table><thead><tr><td>";
                DynamicInterests.Controls.Add(lbl1);
                InterestBean[] interests =  categoriesList[i].interests;
                Label lbl2 = new Label();
                lbl2.Text = categoriesList[i].name;
                DynamicInterests.Controls.Add(lbl2);
                Label lbl3 = new Label();
                lbl3.Text = "</td></tr></thead><tbody>";
                DynamicInterests.Controls.Add(lbl3);
                Panel panel = new Panel();
                for(int j=0; j<interests.Length; j++){
                    Label lbl4 = new Label();
                    lbl4.Text = "<tr><td>";
                    DynamicInterests.Controls.Add(lbl4);
                    CheckBox checkBoxL = new CheckBox();
                    checkBoxL.ID = interests[j].interestId.ToString();
                    checkBoxL.Text = interests[j].name;
                    DynamicInterests.Controls.Add(checkBoxL);
                    Label lbl5 = new Label();
                    lbl5.Text = "</td></tr>";
                    DynamicInterests.Controls.Add(lbl5);
                }
                Label lbl6 = new Label();
                lbl6.Text = "</tbody></table>";
                DynamicInterests.Controls.Add(lbl6);
            }

            /* CHECK according to UserIntersts in DB */
            int [] interestIds = interestService.GetUserInterests(Common.GetUserId(Session));
            for (int i = 0; i < interestIds.Length; i++ )
            {
                Control ctrl = DynamicInterests.FindControl(interestIds[i].ToString());
                ((CheckBox)ctrl).Checked = true;
            }

            AUTO_modifyInterestsBtn.Click += new EventHandler(ModifyBtn_Click); 
        }

        protected void ModifyBtn_Click(object sender, EventArgs e)
        {
            int languageId = Common.GetUserLanguage(Common.GetUserId(Session));
            InterestService interestService = new InterestService();

            interestService.DelInterestsFromUser(Common.GetUserId(Session));

            CategoryBean[] categoriesList = interestService.GetCategoriesList(languageId);
            
            for (int i = 0; i < categoriesList.Length; i++)
            {
                InterestBean[] interests = categoriesList[i].interests;
                for (int j = 0; j < interests.Length; j++)
                {
                    Control ctrl = DynamicInterests.FindControl(interests[j].interestId.ToString());
                    CheckBox tmp = (CheckBox)ctrl;
                    if (tmp.Checked)
                    {
                        interestService.AddInterestToUser(Common.GetUserId(Session), interests[j].interestId);
                    }
                }
            }
        }

    }
}